Return to start page

Core/General/Library Game.j

Code

		
1			library ALibraryCoreGeneralGame
2
3 /// This function I discovered myself (and with the help of Captain Griffen). Simply put, if your
4 /// actually playing a game, it will return true HOWEVER if your viewing the game as a replay it
5 /// will return false. The function has been tested in MultiPlayer and it now causes no desyncs
6 /// (by tested I mean vigorously tested by IceFrog for like 3 months). There is only one
7 /// downside, and that is the function uses up one pause.
8 ///
9 /// There might be a second version of the function that doesn't use up a pause (discovered by
10 /// Strilanc) however there are a couple of issues with it
11 ///
12 /// How does it work?
13 /// Simply put a replay is just a file that contains the inputs from the various players when
14 /// playing a game. However when Wc3 views a replay, there are differences then if you were
15 /// physically playing the game. One of the differences is pausing. If you are actually playing
16 /// and you pause a game, it is paused. However during a replay, if you pause a game the replay
17 /// simply finishes at the point you paused the game, if the game is resumed some time later then
18 /// the replay completely skips the pause phase. There are only a few things that work in real
19 /// time during pauses when your playing the game (anything else you do during the pause just
20 /// gets 'suspended' until the game is resumed). The 2 things that work in real time are camera
21 /// angles and trigger sleep action.
22 ///
23 /// The function works by moving the camera during the pause. If the camera is moved during the
24 /// pause, then you are playing the actual game. If the camera isn't moved then it is the replay
25 /// (since the pause phase is totally skipped)
26 ///
27 /// - It is HIGHLY recommended that you save the returning value of the function into a boolean
28 /// variable i.e.
29 /// set someboolean = IsInGame()
30 /// So you only call the function once (since it takes up a pause)
31 /// @author PandaMine, Captain Griffen
32 function IsInGame takes nothing returns boolean
33 local integer counter = 0
34 local player localPlayer = GetLocalPlayer()
35 local player user
36 local real cameraX
37 local real cameraY
38 local real x
39 local real y
40 local boolean result
41 loop
42 exitwhen (counter == bj_MAX_PLAYERS)
43 set user = Player(counter)
44 if (localPlayer == user) then
45 set cameraX = GetCameraTargetPositionX()
46 set cameraY = GetCameraTargetPositionY()
47 endif
48 set user = null
49 set counter = counter + 1
50 endloop
51 call PauseGame(true)
52 call TriggerSleepAction(0)
53 set counter = 0
54 loop
55 exitwhen (counter == bj_MAX_PLAYERS)
56 set user = Player(counter)
57 if (localPlayer == user) then
58 call SetCameraPosition(cameraX + 1, cameraY + 1)
59 endif
60 set user = null
61 set counter = counter + 1
62 endloop
63 call TriggerSleepAction(0)
64 call PauseGame(false)
65 set counter = 0
66 loop
67 exitwhen (counter == bj_MAX_PLAYERS)
68 set user = Player(counter)
69 if (localPlayer == user) then
70 set x = GetCameraTargetPositionX()
71 if x == cameraX + 1 then
72 set result = true
73 else
74 set result = false
75 endif
76 call SetCameraPosition(cameraX, cameraY)
77 endif
78 set user = null
79 set counter = counter + 1
80 endloop
81 return result
82 endfunction
83
84 /// With the new change to the function, it no longer needs to be synchronized however you should
85 /// still initialize it like this because it has been reported that selecting units +
86 /// triggersleepaction can cause desyncs
87 /// @author PandaMine, Captain Griffen
88 function IsInGameEx takes nothing returns boolean
89 local boolean result
90 call EnableUserControl(false)
91 call TriggerSleepAction(.0)
92 set result = IsInGame()
93 call EnableUserControl(true)
94 return result
95 endfunction
96
97 endlibrary